package com.mzba.place.ui;

import java.util.HashMap;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.mzba.place.BasicActivity;
import com.mzba.place.R;
import com.mzba.place.utils.AppContext;
import com.mzba.place.utils.HttpUtils;
import com.mzba.place.utils.StringUtil;
import com.mzba.place.utils.UICore;
import com.mzba.place.utils.UrlConfig;

/**
 * 
 * @author 06peng
 * 
 */
public class FeedBackActivity extends BasicActivity implements TextWatcher, Callback {

	private final static int MAX_TEXT_SIZE = 255;
	private final static int SYSMES = 1;
	private final static int CONFIRM = 0;
	private final static int CONFIRM_error = 2;

	private EditText feedBackEdit = null;
	private TextView feedBackHint = null;
	private Handler mHanlder = null;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mHanlder = new Handler(this);
		actionBar.show();
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle("意见反馈");
		setSupportProgressBarIndeterminateVisibility(false);
		this.setContentView(R.layout.activity_feed_back);
		feedBackEdit = (EditText) this.findViewById(R.id.feed_back_edit);
		feedBackHint = (TextView) this.findViewById(R.id.feed_back_hint);
		feedBackEdit.addTextChangedListener(this);

		WindowManager.LayoutParams params = this.getWindow().getAttributes();
		this.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, CONFIRM, Menu.NONE, "提交").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		case CONFIRM:
			UICore.eventTask(this, this, CONFIRM, null, true);
			break;
		default:
			break;
		}
		return true;
	}

	@Override
	public boolean handleMessage(Message mes) {
		try {
			switch (mes.what) {
			case CONFIRM:
				this.dialog.dismiss();
				this.finish();
				break;
			case SYSMES:
				setSysMes((String) mes.obj);
				break;
			case CONFIRM_error:
				Toast.makeText(this, "发送失败！", Toast.LENGTH_LONG).show();
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}


	/**
	 * 事务逻辑处理
	 */
	public void execute(int mes, Object obj) {
		try {
			switch (mes) {
			case CONFIRM:
				feedBack();
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		StringBuilder sb = new StringBuilder();
		sb.append(feedBackEdit.getText().toString().length());
		sb.append("/");
		sb.append(MAX_TEXT_SIZE);
		if (feedBackEdit.getText().toString().length() > MAX_TEXT_SIZE) {
			StringBuilder sb2 = new StringBuilder();
			sb2.append("超出");
			sb2.append(feedBackEdit.getText().toString().length() - MAX_TEXT_SIZE);
			sb2.append("个字符");
			feedBackHint.setText(StringUtil.hightLinghtString(sb.toString() + sb2.toString(), sb2.toString(), this.getResources().getColor(R.color.red)));

		} else {
			feedBackHint.setText(sb.toString());
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {

	}

	public void feedBack() {
		String str = feedBackEdit.getText().toString();
		if (str.length() == 0) {
			Message localMessage = Message.obtain(mHanlder, SYSMES, "您没有输入任何信息");
			mHanlder.sendMessage(localMessage);
			return;
		}
		try {
			HashMap<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("auth_token", AppContext.getInstance().getAuthToken());
			paramsMap.put("content", str);
			String rc = HttpUtils.doPost(this, UrlConfig.feedbacks, paramsMap);
			if (StringUtil.isNotBlank(rc)) {
				mHanlder.sendEmptyMessage(CONFIRM);
			} else {
				mHanlder.sendEmptyMessage(CONFIRM_error);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Message localMessage = Message.obtain(mHanlder, SYSMES, "网络问题，信息不能保存到服务器,请稍候再试");
			mHanlder.sendMessage(localMessage);
		}
	}

	@Override
	public void finish() {
		if (getCurrentFocus() != null) {
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		}
		super.finish();
	}

}
